rm(list = setdiff(ls(), lsf.str())) 
load("Study 1/Altered Data/Study1_Swiss_Elections15.RData")

#SI A.10 models belonging to Figure 1----------------
vote.svp15<-list()
vote.svp15[[1]]<-glm(vote_svp ~ zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age + language2 + language3, data=data, family="binomial")
vote.svp15[[2]]<-glm(vote_svp ~ zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age + language2 + language3 + edu2 + edu3 + edu4 + edu5 + edu6 + edu7 + edu8 + edu9 + edu10 + edu11 + edu12 + income + income_missing + zero1(econ_socialspending) + zero1(social_limit_immi), data=data, family="binomial")

labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age" , "Language: (Ref: French) German", "Language: Italian",  "Basic vocational", "Apprenticeship", "Diploma school", "Trading school", "Secondary school", "High school", "Higher vocational (Master)", "Higher vocational (College)", "University of Applied Sciences", "University", "Other", "Income", "Income missing", "Economic conservatism", "Social conservatism")
stargazer2(vote.svp15, odd.ratio=TRUE, title="Swiss Election 2015: Vote for Swiss People's Party", align=TRUE,  omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(0.05), covariate.labels = labels,dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base", "Figure 1"), notes = "Odds ratios with standard errors from logistic regression models; *p<0.05",  notes.append = FALSE, out="Tables/SwissElection15_results.tex", no.space=TRUE, label="tab:SwissElection15_results")

#calculate 1 SD above and below the mean
low  <- mean(zero1(data$agre), na.rm=T)-sd(zero1(data$agre), na.rm=T)
high <- mean(zero1(data$agre), na.rm=T)+sd(zero1(data$agre), na.rm=T)

# Odds ratio
odds.difference <- (exp(vote.svp15[[2]]$coefficients)[2]*low) / (exp(vote.svp15[[2]]$coefficients)[2]*high)
#1/odds.difference

#SI A.10 Correlation--------------
myvars <- c("agre", "open", "con", "ext", "neu" , "social_limit_immi", "econ_socialspending")
cor_ivs <- data[myvars]
names(cor_ivs) <-  c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                     "4. Extraversion", "5. Neuroticism", 
                     "6. Social conservatism", "7. Economic conservatism")
correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
correlation.matrix<-correlation.matrix[,c(1:7)]
names(correlation.matrix) <- seq(1,7,1)
correlation.matrix<-as.matrix(correlation.matrix)
stargazer(correlation.matrix, title="Swiss Election Study 2015: Correlation Matrix of Independent Variables", out="Tables/Swiss15_cor_election.tex", no.space=TRUE, label="tab:Swiss_cor15_election", digits=2)

### Factor loadings------------
latent<-(with(data, data.frame(agr1_rec, agr2, agr3, open1, open2, open3, con1, con2_rec, con3, ext1, ext2, ext3_rec, neu1, neu2, neu3_rec)))
latent <- na.omit(latent)
CFA_agree <-'Agree = ~ agr1_rec+ agr2+ agr3
Open = ~ open1+ open2+ open3
Con = ~ con1+ con2_rec+ con3
Ext = ~ ext1+ ext2+ ext3_rec
Neu = ~ neu1+ neu2+ neu3_rec
Agree ~~ 1*Agree
Open ~~ 1*Open
Con ~~ 1*Con
Ext ~~ 1*Ext
Neu ~~ 1*Neu
' 
fit<-cfa(CFA_agree, data=latent)
summary(fit, fit.measures=TRUE, standardized=T)
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[1:15, ] 
names(p) <- c("Standardized Factor Loading", "p-value")
(setattr(p, "row.names", c("Agreeableness item 1", "Agreeableness item 2", "Agreeableness item 3", "Openness item 1", "Openness item 2", "Openness item 3", "Conscientiousness item 1", "Conscientiousness item 2", "Conscientiousness item 3",  "Extraversion item 1", "Extraversion item 2", "Extraversion item 3", "Neuroticism item 1", "Neuroticism item 2", "Neuroticism item 3")))

p<-xtable(caption = "Swiss Election Study 2015: Big Five Standardized Factor Loadings", label = "tab:Swiss15_cfa_election", p)
print(p, type="latex", file="Tables/Swiss15_cfa_election.tex", size="tiny", caption.placement="top")

###Descriptive statistics-----
desc.labels <- c("Populist Vote", labels)
stargazer(vote.svp15[[2]]$model, type = "latex", covariate.labels=desc.labels, summary.stat = c("mean", "sd", "median","min",  "max"), title="Descriptive statistics Swiss Election Study 2015", out="Tables/Swiss_descrip15_election.tex", no.space=TRUE, label="tab:Swiss_descriptives15_election", digits=2)

#Alpha Agreeableness
alpha_a<-data.frame(data$agr1_rec, data$agr2, data$agr3)
psych::alpha(alpha_a)

#Alpha Openness
alpha_o<-data.frame(data$open1, data$open2, data$open3)
psych::alpha(alpha_o)
#Alpha Conscientiousness
alpha_c<-data.frame(data$con1, data$con2_rec, data$con3)
psych::alpha(alpha_c)
#Alpha Extraversion
alpha_e<-data.frame(data$ext1, data$ext2, data$ext3_rec)
psych::alpha(alpha_e)
#Alpha Neuroticism
alpha_n<-data.frame(data$neu1, data$neu2, data$neu3_rec)
psych::alpha(alpha_n)
